﻿
using DevExpress.CodeParser;
using DevExpress.LookAndFeel;
using DevExpress.Office;
using DevExpress.Office.Utils;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.ColorWheel;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraNavBar;
using DevExpress.XtraSplashScreen;
using DevExpress.XtraWaitForm;
using Furion;
using Furion.Logging;
using GnpFine.Application.NmpaSystem;
using GnpFine.Core;
using Kingdee.SDK.com.kingdee.service.data.entity;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Controls.Ribbon;
using System.Windows.Forms;

namespace GnpFine.StartUI.WinFormFu
{
    public partial class MainForm : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        private readonly List<NavMenu> navMenus = new List<NavMenu>()
                    {
                        new NavMenu { Id = 1, Name="wid1", DisName="导航1",Ico="Icons\\AcceptRefuse.ico", Pid=0 } ,
                        new NavMenu { Id = 2, Name="wid2", DisName="导航2",Ico="Icons\\AcceptRefuse.ico", Pid=0 } ,
                        new NavMenu { Id = 3, Name="wid3", DisName="导航3",Ico="Icons\\AcceptRefuse.ico", Pid=0 } ,
                        new NavMenu { Id = 11,Name="ceshi1", DisName="测试1",Ico="Icons\\AcceptRefuse.ico", FormName="GnpFine.StartUI.WinFormFu.CeshiForm", Pid=1 } ,
                        new NavMenu { Id = 12,Name="ceshi2", DisName="测试2",Ico="Icons\\AcceptRefuse.ico", FormName="GnpFine.StartUI.WinFormFu.CeshiForm", Pid=1 } ,
                        new NavMenu { Id = 13,Name="ToolbarForm1", DisName="ToolbarForm1",Ico="Icons\\AcceptRefuse.ico", FormName="GnpFine.StartUI.WinFormFu.ToolbarForm1", Pid=1 } ,
                        new NavMenu { Id = 14,Name="rsa", DisName="rsa测试",Ico="Icons\\AcceptRefuse.ico", FormName="GnpFine.StartUI.WinFormFu.RegServices", Pid=1 } ,
                    };

        private readonly IServiceScopeFactory _scopeFactory;
        private readonly ISqlSugarClient _db;

        #region 窗口的样式，初始化，固定代码
/*        public MainForm(IServiceScopeFactory scopeFactory, ISqlSugarClient db)
        {
            _scopeFactory = scopeFactory;
            _db = db;
*/  
        public MainForm()   
        {

            InitializeComponent();

            this.IsMdiContainer = true;
            xtraTabbedMdiManager1.MdiParent = this; //设置控件的父表单..


            this.Text = "主界面";
            this.MaximizeBox = true;
            this.WindowState = FormWindowState.Maximized;

            //菜单条设置
            ribbonControl1.MdiMergeStyle = RibbonMdiMergeStyle.Always;
            ribbonControl1.RibbonStyle = RibbonControlStyle.Office2010;
            //ribbon.Minimized = true;

            //导航列表设置
            InitNavbar();
        }




        /// <summary>
        /// 初始化导航控件
        /// </summary>
        private void InitNavbar()
        {
            navBarControl1.Groups.Clear();
            navBarControl1.NavigationPaneMaxVisibleGroups = 6;
            navBarControl1.PaintStyleKind = NavBarViewKind.NavigationPane;
            navBarControl1.ForeColor = Color.Black;
            navBarControl1.BackColor = Color.Black;
            navBarControl1.BorderStyle = BorderStyles.Simple;




            var rootMenu = navMenus.Where(t => t.Pid == 0).ToList();

            foreach (var srm in rootMenu)
            {
                //导航列表组设置增加
                NavBarGroup navBarGroup = new NavBarGroup();
                navBarGroup.Name = srm.Name;
                navBarGroup.Caption = srm.DisName;
                navBarGroup.Appearance.BackColor = System.Drawing.Color.Black;
                navBarGroup.Appearance.BackColor2 = System.Drawing.Color.FromArgb(((int)(((byte)(224)))),
                    ((int)(((byte)(224)))), ((int)(((byte)(224)))));
                navBarGroup.Appearance.BorderColor = System.Drawing.Color.Blue;
                navBarGroup.Appearance.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold,
                    System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                navBarGroup.Appearance.ForeColor = System.Drawing.Color.Gray;
                navBarGroup.Appearance.Options.UseBackColor = true;
                navBarGroup.Appearance.Options.UseBorderColor = true;
                navBarGroup.Appearance.Options.UseFont = true;
                navBarGroup.Appearance.Options.UseForeColor = true;
                navBarGroup.AppearanceBackground.BackColor = System.Drawing.Color.Red;
                navBarGroup.AppearanceBackground.BackColor2 = System.Drawing.Color.Red;
                navBarGroup.AppearanceBackground.Font = new System.Drawing.Font("仿宋", 9F, System.Drawing.FontStyle.Bold);
                navBarGroup.AppearanceBackground.Options.UseBackColor = true;
                navBarGroup.AppearanceBackground.Options.UseFont = true;

                navBarGroup.Expanded = true;
                navBarControl1.Groups.Add(navBarGroup);


                var subMenus = navMenus.Where(t => t.Pid == srm.Id).ToList();

                foreach (var srmd in subMenus)
                {
                    NavBarItem navBarItem1 = new NavBarItem();
                    navBarItem1.Name = srmd.Name;
                    navBarItem1.Caption = srmd.DisName;

                    //navBarItem1.SmallImage = Image.FromFile(srmd.Ico);
                    //string strFilePath = System.IO.Directory.GetCurrentDirectory() + @"\images\add.png";
                    // _navBarItem.LargeImage = Image.FromFile(strFilePath);//这里一直无法显示图片，不知道是不是我写错了还是什么
                    navBarItem1.LargeImageIndex = 0;
                    navBarItem1.LinkClicked += new NavBarLinkEventHandler(birt_LinkClicked);
                    navBarGroup.ItemLinks.Add(navBarItem1);
                }
            }

        }


        /// <summary>
        /// 导航按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void birt_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            string menuName = e.Link.Item.Name;
            string menuCaption = e.Link.Caption;

            string formName = navMenus.Where(t => t.Name.Equals(menuName)).First().FormName.ToString();


            //MessageBox.Show(FormName + "   " + FormLink1);
            ActivateOrAddForm(menuCaption, formName);
        }

        private void ActivateOrAddForm(string menuCaption, string ViewType)
        {
            foreach (Form subForm in this.MdiChildren)
            {
                if (subForm.Text == menuCaption)
                {
                    subForm.Activate();
                    return;
                }
            }

            Form fm = (Form)Activator.CreateInstance(Type.GetType(ViewType));
            fm.MdiParent = this;
            fm.Text = menuCaption;
            fm.Show();


            xtraTabbedMdiManager1.SelectedPage = xtraTabbedMdiManager1.Pages[fm]; //使得标签的选择为当前新建的窗口
            xtraTabbedMdiManager1.ClosePageButtonShowMode = DevExpress.XtraTab.ClosePageButtonShowMode.InAllTabPagesAndTabControlHeader;
            //设置标签后面添加删除按钮 ,  多个标签只需要设置一次..



        }



        private void bbColorMix_ItemClick(object sender, ItemClickEventArgs e)
        {
            ColorWheelForm form = new ColorWheelForm();
            form.StartPosition = FormStartPosition.CenterParent;
            form.SkinMaskColor = UserLookAndFeel.Default.SkinMaskColor;
            form.SkinMaskColor2 = UserLookAndFeel.Default.SkinMaskColor2;
            form.ShowDialog(this);
        }



        private SplashScreenManager _loadForm;
        /// <summary>
        /// 等待窗体管理对象
        /// </summary>
        protected SplashScreenManager LoadForm
        {
            get
            {
                if (_loadForm == null)
                {
                    this._loadForm = new SplashScreenManager(this, typeof(WaitForm1), true, true);
                    this._loadForm.ClosingDelay = 0;
                }
                return _loadForm;
            }
        }
        /// <summary>
        /// 显示等待窗体
        /// </summary>
        public void ShowMessage()
        {
            bool flag = !this.LoadForm.IsSplashFormVisible;
            if (flag)
            {
                this.LoadForm.ShowWaitForm();
            }
        }
        /// <summary>
        /// 关闭等待窗体
        /// </summary>
        public void HideMessage()
        {
            bool isSplashFormVisible = this.LoadForm.IsSplashFormVisible;
            if (isSplashFormVisible)
            {
                this.LoadForm.CloseWaitForm();
            }
        }
    #endregion 窗口的样式，初始化，固定代码


        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            // 一 、仓储方法
            var instrumentStoreRep = App.GetService<SqlSugarRepository<InstrumentStock>>();
            var list = instrumentStoreRep.AsQueryable().ToList();


            // 二 、单例查询
            ISqlSugarClient db = App.GetService<ISqlSugarClient>();//用手动获取方式支持切换仓储
            var _childMain = db.AsTenant().GetConnection(SqlSugarConst.MainConfigId);
            int pageIndex = 1;
            int pageSize = 20;
            int totalCount = 0;
            var page = _childMain.Queryable<SysLogOp>().ToPageList(pageIndex, pageSize, ref totalCount);
            List<SysLogOp> sysLogOps = page.ToList();
            sysLogOps.ForEach(u => Log.Information("log的数据集" + u.ToString()));

            // 自动切库 切表
            var ll=db.AsTenant().QueryableWithAttr<InstrumentStock>().ToList();

            // 四 、原生Sql操作 ORM操作 Sql
            // 下面的方法支持复杂的Sql  、 返回多个结果集 、存储过程等  、可以理解为了一个高级DbHelper
            ISqlSugarClient dbs = App.GetService<ISqlSugarClient>();//用手动获取方式支持切换仓储
            var dbs1 = dbs.AsTenant().GetConnection(SqlSugarConst.NmpaConfigId);

            //无实体查询返回datatable
            var dt = dbs1.Ado.GetDataTable("SELECT * from InstrumentStore");
            /*复杂查询
            var dt = dbs.Ado.GetDataTable("select * from table where id=@id and name like @name",
            new List<SugarParameter>(){
                  new SugarParameter("@id",1),
                  new SugarParameter("@name","%"+jack+"%") //执行sql语句
            });*/
            //比db.SqlQueryable兼容性更强，支持复杂SQL存储过程，缺点没有自带的分页操作
            List<InstrumentStock> t = dbs1.Ado.SqlQuery<InstrumentStock>("SELECT * from InstrumentStore");

            // 数据库优先，可以自动生成实体类
            //db.DbFirst.IsCreateAttribute().CreateClassFile(@"D:\xxx\models", "Models");



            DialogResult dr = XtraMessageBox.Show("ok", "警告", MessageBoxButtons.YesNoCancel);

            if (dr == DialogResult.Yes)
            {
                DialogResult dr1 = XtraMessageBox.Show("werw", "警告", MessageBoxButtons.YesNoCancel);
                //_logger.LogInformation("日志注入成功!!---------+++++++++++++=========@@@@@@@");
            }


        }
        // 








        }




    /// <summary>
    /// 导航菜单的结构封装
    /// </summary>
    public class NavMenu
    {

        [DisplayName("ID")]
        public int Id { get; set; }

        [DisplayName("名称")]
        public string Name { get; set; }

        [DisplayName("显示名称")]
        public string DisName { get; set; }

        [DisplayName("图标")]
        public string Ico { get; set; }

        [DisplayName("类名")]
        public string FormName { get; set; }

        [DisplayName("父ID")]
        public int Pid { get; set; }

        [DisplayName("是否明细")]
        public bool Fdetail { get; set; }

        [DisplayName("顺序")]
        public int Forder { get; set; }

        [DisplayName("备注")]
        public string Fmemo { get; set; }

    }


}

